<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src/dist/css/layui.css">
    <style>
        .layui-container {
            margin-top: 100px;
        }
    </style>
</head>
<body>
<div class="layui-container">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md6 layui-col-md-offset3">
            <div class="layui-card">
                <div class="layui-card-header">登录</div>
                <div class="layui-card-body">
                    <form class="layui-form" id="loginForm">
                        <div class="layui-form-item">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-block">
                                <input type="text" name="phone" required lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">密码</label>
                            <div class="layui-input-block">
                                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">角色</label>
                            <div class="layui-input-block">
                                <select name="role" required lay-verify="required">
                                    <option value="0">管理员</option>
                                    <option value="1">陪诊员</option>
                                    <option value="2">患者</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">立即登录</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/layui-src/dist/layui.js"></script>
<script>
    layui.use(['form', 'layer'], function(){
        var form = layui.form;
        var layer = layui.layer;

        // 监听提交
        form.on('submit(formDemo)', function(data){
            $.ajax({
                url: 'http://localhost:8080/auth/login',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function(res){
                    if(res.code === 200){
                        layer.msg('登录成功', {icon: 1, time: 1000}, function(){
                            window.location.href = '/'; // 登录成功后跳转的页面
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function(){
                    layer.msg('系统错误', {icon: 2});
                }
            });
            return false; // 阻止表单跳转
        });
    });
</script>
</body>
</html>
